/*
 * ========================================================
 * Copyright(c) 2014 杭州偶尔科技-版权所有
 * ========================================================
 * 本软件由杭州偶尔科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * 
 * 			http://www.kkkd.com/
 * 
 * ========================================================
 */
package com.simon.mengkou.ui.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.simon.mengkou.R;
import com.simon.mengkou.data.bean.base.Coupon;
import com.simon.mengkou.data.bean.base.Order;
import com.simon.mengkou.data.bean.base.Product;
import com.simon.mengkou.data.enums.EOrderStatus;
import com.simon.mengkou.ui.view.MyListView;
import com.simon.mengkou.utils.UtilOuer;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @author : Zhenshui.Xia
 * @since : 2016/3/25.
 * desc    :
 */
public class CouponAdapter extends AbsAdapter<Coupon> {
    private int mRedColor;
    private int mSecondaryGrayColor;
    private int mHintGrayColor;

    public CouponAdapter(Context context) {
        super(context);
        mRedColor = ContextCompat.getColor(context, R.color.res_color_primary);
        mSecondaryGrayColor = ContextCompat.getColor(context, R.color.res_color_black_secondary_text);
        mHintGrayColor = ContextCompat.getColor(context, R.color.res_color_black_hint_text);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {
        final Holder holder;
        if (null == convertView) {
            convertView = mInflater.inflate(R.layout.layout_coupon_item, null);
            holder = new Holder();
            convertView.setTag(holder);
            ButterKnife.bind(holder, convertView);
        } else {
            holder = (Holder) convertView.getTag();
        }

        final Coupon data = getItem(position);

        if(isEffective(data)) {
            //背景
            holder.mLlRoot.setBackgroundResource(R.drawable.coupon_bg_effective);
            //货币符号
            holder.mIvSymbol.setImageResource(R.drawable.coupon_ic_symbol_effective);
            //价值
            holder.mTvAmount.setText(String.valueOf((int)data.getAmount()));
            holder.mTvAmount.setTextColor(mRedColor);
            //优惠券名字
            holder.mTvName.setText(data.getName());
            holder.mTvName.setTextColor(mRedColor);
            holder.mTvName.setBackgroundResource(R.drawable.common_bg_red_line_btn_normal);
            //是否失效
            holder.mIvEffective.setVisibility(View.GONE);
            //描述
            holder.mTvDesc.setText(mContext.getString(R.string.coupon_desc, 11, 22));
        } else {
            holder.mLlRoot.setBackgroundResource(R.drawable.coupon_bg_uneffective);
            holder.mIvSymbol.setImageResource(R.drawable.coupon_ic_symbol_uneffective);
            holder.mTvAmount.setText(String.valueOf((int)data.getAmount()));
            holder.mTvAmount.setTextColor(mSecondaryGrayColor);
            holder.mTvName.setText(data.getName());
            holder.mTvName.setTextColor(mHintGrayColor);
            holder.mTvName.setBackgroundResource(R.drawable.common_bg_gray_line_btn_normal);
            holder.mIvEffective.setVisibility(View.VISIBLE);
            holder.mTvDesc.setText(mContext.getString(R.string.coupon_desc, 11, 22));
        }

        return convertView;
    }


    /**
     * @author zhenshui.xia
     */
    class Holder {
        //
        @Bind(R.id.coupon_id_bg)
        LinearLayout mLlRoot;
        //货币符号
        @Bind(R.id.coupon_id_symbol)
        ImageView mIvSymbol;
        //价值
        @Bind(R.id.coupon_id_amount)
        TextView mTvAmount;
        //优惠券名字
        @Bind(R.id.coupon_id_name)
        TextView mTvName;
        //是否失效
        @Bind(R.id.coupon_id_uneffective)
        ImageView mIvEffective;
        //描述
        @Bind(R.id.coupon_id_desc)
        TextView mTvDesc;
    }

    /**
     * 优惠券是否有效
     * @param coupon
     * @return
     */
    private boolean isEffective(Coupon coupon) {
        return System.currentTimeMillis() < coupon.getEndTime();
    }
}
